package com.ruoyi.project.api.user;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.course.course.domain.BCourse;
import com.ruoyi.project.course.course.service.IBCourseService;
import com.ruoyi.project.courseuser.courseuser.mapper.BCourseUserMapper;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程
 *
 */

@RestController
@RequestMapping("/api/course")
public class WebCourseController extends BaseController {
    @Autowired
    IBCourseService courseService;
    @Autowired
    private BCourseUserMapper bCourseUserMapper;

    @ApiOperation("课程列表")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @PostMapping("/list")
    public TableDataInfo list(BCourse bCourse) {
        startPage();
        List<BCourse> list = courseService.selectBCourseList(bCourse);
        return getDataTable(list);
    }

    @ApiOperation("课程详情")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @GetMapping("/findCourseById")
    public AjaxResult findCourseById( Long id) {
        BCourse bCourse = courseService.selectBCourseById(id);
        //检查是否报名人数足够
        Integer count = bCourseUserMapper.findBCourseUseByCourseId(id);
        bCourse.setUserCount(0);
        if(!ObjectUtils.isEmpty(count)){
            bCourse.setUserCount(count);
        }
        return AjaxResult.success(bCourse);
    }
}
